package com.hou.selector;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

//生产者
public class Provider {
    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory(
                "houzheng",//默认的用户
                "houzheng",
                "tcp://localhost:61616"  //默认端口: 61616
        );
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
        Destination destination=session.createQueue("myqueue");
        MessageProducer producer = session.createProducer(destination);//传入Destination
        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        //发送消息
        for (int i = 0; i < 5; i++) {
            TextMessage textMessage = session.createTextMessage();
            //设置Selector选择器可以过滤的属性,一定要带Property才能被过滤,所有属性均可设置
            if((i&1)!=1){//奇数
                textMessage.setStringProperty("name","侯征");
                textMessage.setDoubleProperty("money",1000*i);
            }else{//偶数
                textMessage.setStringProperty("name","姚振");
                textMessage.setDoubleProperty("money",500*i);
            }
            producer.send(textMessage);
        }
        //如果开启了事务,则必须要提交才能全部发送到服务器
        //session.commit();
        if(connection!=null)connection.close();
    }
}
